import 'dart:async';

import 'package:camera/camera.dart';
import 'package:get/get.dart';

class GlobalService extends GetxService {
  final isImmerse = false.obs; // 当前是否在动态详情，用于改变底部导航栏图标
  final _countdown = 0.obs;

  late List<CameraDescription> cameras = RxList(); // 可用相机列表
  Timer? _timer;

  int get countdown => _countdown.value;

  @override
  onInit() {
    _fetchCameras();
    super.onInit();
  }

  Future<void> _fetchCameras() async {
    cameras = await availableCameras();
  }

  void startCountdown({int countdown = 60}) {
    destroyTimer();
    _countdown.value = countdown;

    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (_countdown <= 0) {
        timer.cancel();
      } else {
        _countdown.value--;
      }
    });
  }

  void destroyTimer() {
    _timer?.cancel();
    _countdown.value = 0;
  }
}
